package com.appspot.codelab.docs.client.content.dialogs;

import com.appspot.codelab.docs.client.events.PasswordSelectionEvent;
import com.appspot.codelab.docs.client.events.PasswordSelectionHandler;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.RunAsyncCallback;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.PasswordTextBox;

/**
 * A dialog window containing a form for selecting a project.
 */
public class DynamicPasswordSelectionDialog extends DynamicFormDialog {

  /**
   * Contains form fields for selecting a project.
   */
  protected class ProjectSelectionFormContents extends FormContents {
	
	protected Anchor projectName, projectUser;
	protected PasswordTextBox projectPassword;
	protected CheckBox rememberPassword;
	
    /**
     * Builds the password form.
     */
	@Override
	protected void buildForm() {
	  content.setWidth("600px");
	  projectName = new Anchor(
		  DynamicPasswordSelectionDialog.this.projectName,
		  "http://code.google.com/p/" + DynamicPasswordSelectionDialog.this.projectName);
	  projectName.setTarget("_blank");
	  projectUser = new Anchor(
		  DynamicPasswordSelectionDialog.this.projectUser,
		  "http://code.google.com/u/" + DynamicPasswordSelectionDialog.this.projectUser);
	  projectUser.setTarget("_blank");
	  addField(projectName, "Project Name");
	  addField(projectUser, "Project User");
	  projectPassword = new PasswordTextBox();
	  projectPassword.setWidth("200px");
	  addField(projectPassword, "Project Password");
	  rememberPassword = new CheckBox("Remember project password for this session");
	  addField(rememberPassword);
      submit.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
          if (projectPassword.getValue().equals("")) {
        	Window.alert("Project password is required.");
        	return;
          }
          hide();
          selectionHandler.onSelection(
    	      new PasswordSelectionEvent(
    	          projectPassword.getValue(),
    	          rememberPassword.getValue()));
        }
      });
      cancel.addClickHandler(new ClickHandler(){
          public void onClick(ClickEvent event) {
            hide();
          }
      });
	}
  
    /**
     * Resets the form fields to their default values.
     */
    @Override
    public void resetForm() {
      this.projectName.setText(DynamicPasswordSelectionDialog.this.projectName);
      this.projectName.setHref(
    	  "http://code.google.com/p/" + DynamicPasswordSelectionDialog.this.projectName);
      this.projectUser.setText(DynamicPasswordSelectionDialog.this.projectUser);
      this.projectUser.setHref(
    	  "http://code.google.com/u/" + DynamicPasswordSelectionDialog.this.projectUser);
    }

  }

  protected static DynamicPasswordSelectionDialog instance;
  
  /**
   * Retrieves the single instance of this class, using asynchronous instantiation.
   */
  public static DynamicPasswordSelectionDialog get(
		String projectName,
		String projectUser,
		PasswordSelectionHandler selectionHandler) {
    if (instance == null) {
      instance = new DynamicPasswordSelectionDialog();
    }
    instance.selectionHandler = selectionHandler;
    instance.projectName = projectName;
    instance.projectUser = projectUser;
    return instance;
  }
  
  protected String projectName, projectUser;
  protected PasswordSelectionHandler selectionHandler;
  
  /**
   * Constructs a dialog window containing a form for specifying a project password.
   */
  public DynamicPasswordSelectionDialog() {
    super("Project Password", true, "600px", null);
  }

  /**
   * Retrieves the dialog's contents asynchronously.
   * 
   * @param callback the callback carrying the content widget
   */
  @Override
  protected void getFormContents(final AsyncCallback<FormContents> callback) {
    GWT.runAsync(new RunAsyncCallback() {
		@Override
		public void onFailure(Throwable reason) {
		  callback.onFailure(reason);
		}
		@Override
		public void onSuccess() {
	      ProjectSelectionFormContents form = new ProjectSelectionFormContents();
	      form.resetForm();
		  callback.onSuccess(form);
		}
    });
  }

}
